/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import { SmsMonitorGetExcel } from '@/services/yz/smsMonitor';
import { UploadOutlined } from '@ant-design/icons';
import UploadWithCrypto from '@base/components/upload-width-crypto';
import { Button, message, Modal } from 'antd';
import React from 'react';

const SmsUpload: React.FC<{ onReload?: () => void }> = (props) => {
  const { onReload } = props;
  const onGetFile = async () => {
    const { code, msg, data = {} } = await SmsMonitorGetExcel({});
    if (code == 0 && data.fail) {
      window.open('//upload.kuaidihelp.com' + data.file);
    } else {
      message.error(msg);
    }
  };
  const propss = {
    name: 'file',
    action: '/Inn/upload',
    showUploadList: false,
    onChange(info: { file: { status: string; name: any; response: any }; fileList: any }) {
      if (info.file.status !== 'uploading') {
        console.log(info.file, info.fileList);
      }
      if (info.file.status === 'done') {
        console.info('info.file.response', info.file.response);
        const { code, msg } = info.file.response || {};
        if (code == 0) {
          if (onReload) {
            onReload();
          }
          Modal.confirm({
            title: '导入完成，请查看导入结果',
            centered: true,
            onOk: onGetFile,
            okText: '下载导入结果',
          });
        } else {
          message.error(msg);
        }
      } else if (info.file.status === 'error') {
        console.info('error', info);
        message.error(`${info.file.name} file upload failed.`);
      }
    },
  };

  return (
    <UploadWithCrypto {...propss}>
      <Button icon={<UploadOutlined />} type='primary'>
        导入
      </Button>
    </UploadWithCrypto>
  );
};

export default SmsUpload;
